package com.asiainfo.lop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Created by wucm(wucm@asiainfo.com) on 2016/6/2 20:34.
 */
public class CrowdDetailListAdapt extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private JSONArray data;

    public CrowdDetailListAdapt(Context context, JSONArray data) {
        super();
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = inflater.inflate(R.layout.crowd_detail_list_item, null);
            vh.img = (ImageView) convertView.findViewById(R.id.crowd_detail_list_item_img);
            vh.title = (TextView) convertView.findViewById(R.id.crowd_detail_list_item_title);
            vh.info = (TextView) convertView.findViewById(R.id.crowd_detail_list_item_info);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        JSONObject d = data.getJSONObject(position);
        String typeId = d.getString("typeId");
        String groupId = d.getString("groupId");
        String groupName = d.getString("groupName");
        String areaName = d.getString("areaName");
        String time = d.getString("time");

        // 聚集
        int imgsrc = R.drawable.crowd_detail_list_item_attent_img;
        String title = "{groupName} 聚集位置 {areaName}";
        String info = "聚集停留时间 {time} 分钟";

        // 进入
        if ("2".equals(typeId)) {
            imgsrc = R.drawable.crowd_detail_list_item_enter_img;
            title = "{groupName} 进入 {areaName}";
            info = "进入时间 {time} ";
        }

        title = title.replace("{groupName}", groupName);
        title = title.replace("{areaName}", areaName);
        info = info.replace("{time}", time);

        vh.img.setImageResource(imgsrc);
        vh.title.setText(title);
        vh.info.setText(info);

        return convertView;
    }

    static class ViewHolder {
        public ImageView img;
        public TextView title;
        public TextView info;
    }
}
